﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;
using System.Collections;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI.Fluent;

namespace System.Web.Mvc.Html
{
    public static class DisplayExtensions2
    {
        public static MvcHtmlString DisplayItemFor<TModel, TCollection, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TCollection>> expression, int index) where TCollection : IEnumerable<TValue>
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            Func<TModel, TCollection> func = expression.Compile();
            TModel model = html.ViewData.Model;
            TCollection values = func(model);
            TValue value = values.ElementAt(index);
            return new MvcHtmlString(value.ToString());
        }
        public static MvcHtmlString DisplayItemPropertyFor<TModel, TCollection, TItem, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TCollection>> expression, int index,
            Expression<Func<TItem, TValue>> expression2) where TCollection : IEnumerable<TItem>
        {
            string name = ExpressionHelper.GetExpressionText(expression);
            StringBuilder sb = new StringBuilder(name);
            sb.Append("[");
            sb.Append(index);
            sb.Append("].");
            sb.Append(ExpressionHelper.GetExpressionText(expression2));
            name = sb.ToString();
            Func<TModel, TCollection> func = expression.Compile();
            Func<TItem, TValue> func2 = expression2.Compile();
            TModel model = html.ViewData.Model;
            TCollection values = func(model);
            TItem item = values.ElementAt(index);
            TValue value = func2(item);
            return new MvcHtmlString(value.ToString());

        }
        public static GridBoundColumnBuilder<TModel> BoundDateTimeFormat<TModel, TValue>(this GridColumnFactory<TModel> cf, Expression<Func<TModel, TValue>> expression, string format) where TModel : class
        {
            string name = expression.GetExpressionText();
            string tmpl = string.Format("<#= typeof({0})=='undefined' ? '': Date.parseExact({0}.substr(0, 19), 'yyyy-MM-ddTHH:mm:ss').toString('{1}') #>", name, format);
            return cf.Bound(expression).ClientTemplate(tmpl);
        }

        /// <summary>
        /// 绑定时间显示
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="cf"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static GridBoundColumnBuilder<TModel> BoundDate<TModel, TValue>(this GridColumnFactory<TModel> cf, Expression<Func<TModel, TValue>> expression) where TModel : class
        {
            return cf.BoundDateTimeFormat(expression, "MM/dd/yyyy");
        }

        /// <summary>
        /// 绑定日期时间显示
        /// </summary>
        /// <typeparam name="TModel"></typeparam>
        /// <typeparam name="TValue"></typeparam>
        /// <param name="cf"></param>
        /// <param name="expression"></param>
        /// <returns></returns>
        public static GridBoundColumnBuilder<TModel> BoundDateTime<TModel, TValue>(this GridColumnFactory<TModel> cf, Expression<Func<TModel, TValue>> expression) where TModel : class
        {
            return cf.BoundDateTimeFormat(expression, "MM/dd/yyyy HH:mm:ss");
        }
        //public static MvcHtmlString DisplayItemFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object index, object additionalViewData) where TValue : IEnumerable;
        //public static MvcHtmlString DisplayItemFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object index, string templateName) where TValue : IEnumerable;
        //public static MvcHtmlString DisplayItemFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object index, string templateName, object additionalViewData) where TValue : IEnumerable;
        //public static MvcHtmlString DisplayItemFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object index, string templateName, string htmlFieldName) where TValue : IEnumerable;
        //public static MvcHtmlString DisplayItemFor<TModel, TValue>(this HtmlHelper<TModel> html, Expression<Func<TModel, TValue>> expression, object index, string templateName, string htmlFieldName, object additionalViewData) where TValue : IEnumerable;
    }
}
